layui.config({
    base: "static/layui/module/"
}).extend({
    dynamicCondition: 'dynamicCondition/dynamicCondition'
}).extend({
    excel: 'excel/excel'
}).use(['form','layer','jquery','table','laydate','element','laytpl','excel','dynamicCondition'],function() {
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table,
        laydate = layui.laydate,
        element = layui.element,
        laytpl = layui.laytpl,
        excel = layui.excel,
        dynamicCondition=layui.dynamicCondition;

    var filter = [];
    var tableCols = object.cols;
    for(var i=0;i<object.power.length;i++){
        var power = object.power[i];
        //使用闭包方式把数据写入内存中
        (function(index,c){
            c.minWidth = 120;
            c.templet =function(d){
                return "<input type='checkbox' name='"+c.field+"' lay-filter='"+c.field+"' lay-skin='switch'  lay-text='是|否' "+(d[c.field]=='Y'?'checked':'')+">";
            };
            tableCols.push(c);
        })(i,power);
        filter.push(object.power[i].field);
    }
    table.render({
        elem: '#table'
        ,title: '列表'
        ,url: '/manage/equipment_power/list/'
        ,method: 'post'
        ,height: 'full-60'
        ,page: true //开启分页
        ,limit: 15
        ,limits: [15, 30, 40, 50, 60, 70, 80, 90, 100]
        ,loading: true
        ,cols: [object.cols]
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        ,toolbar: '#tableToolbar'
        ,defaultToolbar: ['filter']
        ,done:function(){
            for(var i=0;i<filter.length;i++){
                form.on('switch('+filter[i]+')', function(data){
                    top.layer.confirm(data.elem.checked?'确认为该设备接入此动燃？':'确认为该设备取消此动燃？', {icon: 3, title:'提示',btn2: function (index,layero){
                                table.reload('table');
                            }
                        }
                        , function(index){
                        var equipmentId = $(data.othis.parents("tr")).find("td:eq(0)").find(".layui-table-cell").text();
                        var elementsId = data.elem.name;
                        var selectData = {
                            "equipmentId":equipmentId,
                            "elementsId":elementsId
                        };
                        $.post("/manage/equipment_power/change", {obj: JSON.stringify(selectData), type: data.elem.checked},function(d){
                            if(d.code == '200'){
                                layer.msg("操作成功！");
                            }else{
                                layer.msg("操作失败！");
                            }
                            table.reload('table');
                            top.layer.close(index);
                        },"json");
                    })
                });
            }
        }
    });

    var dataFields = [
        {field:"cost_equipment.id", title:"设备",ops:"equal", edit:"select",templet:"#equipment"}
    ];

    var dc = dynamicCondition.create({
        fields:dataFields
        ,tableId:"table"
        ,conditionTextId:"#findMsg"
        ,type:"complex"
        ,requestDataType:"json"
        ,counts:2
        ,queryCallBack:function(requestData){
        }
    });
    table.on('toolbar(table)', function(obj) {
        switch (obj.event) {
            case 'find':
                dc.open();
                break;
        }
    })
})